import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QComboBox, QPushButton, QVBoxLayout, QHBoxLayout,QMessageBox
import email as e

class MyWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        # 创建3个LineEdit
        self.releaseVersion_lineEdit = QLineEdit()
        self.path_lineEdit = QLineEdit()
        self.emailTo_lineEdit = QLineEdit()
        self.emailTo_lineEdit.setPlaceholderText("多个收件人使用;隔开")
        self.emailP_lineEdit = QLineEdit()
        # self.emailCc_comboBox = QComboBox()
        self.emailTo_lineEdit.setText("jimmy.tian1@magna.com")
        self.emailP_lineEdit.setText("Cindy")
        # self.emailTo_comboBox.addItem("Cindy.liu@magna.com")
        # self.emailCc_comboBox.addItem("jimmy.tian1@magna.com")
        # self.emailCc_comboBox.addItem("Xiang.song@magna.com")

        # 创建1个ComboBox
        self.testType_comboBox = QComboBox()
        self.testType_comboBox.addItem("smokeTest")

        # 创建4个Label
        self.label1 = QLabel("ReleaseVersion")
        self.label2 = QLabel("Path")
        self.label3 = QLabel("EmailTo")
        self.label4 = QLabel("TestType")

        # 创建1个PushButton
        self.creat_pushButton = QPushButton("create")

        # 创建一个垂直布局
        layout = QVBoxLayout()

        # 创建一个水平布局
        hlayout1 = QHBoxLayout()
        hlayout1.addWidget(self.label1)
        hlayout1.addWidget(self.releaseVersion_lineEdit)
        # 创建一个水平布局
        hlayout2 = QHBoxLayout()
        hlayout2.addWidget(self.label2)
        hlayout2.addWidget(self.path_lineEdit)
        # 创建一个水平布局
        hlayout3 = QHBoxLayout()
        hlayout3.addWidget(self.label3)
        hlayout3.addWidget(self.emailTo_lineEdit)
        hlayout4 = QHBoxLayout()
        hlayout4.addWidget(self.label4)
        hlayout4.addWidget(self.testType_comboBox)
        hlayout5 = QHBoxLayout()
        hlayout5.addWidget(self.emailP_lineEdit)
        hlayout5.addWidget(self.creat_pushButton)
        layout.addLayout(hlayout1)
        layout.addLayout(hlayout2)
        layout.addLayout(hlayout3)
        layout.addLayout(hlayout4)
        layout.addLayout(hlayout5)
        # layout.addWidget(self.creat_pushButton)

        self.creat_pushButton.clicked.connect(self.generalEmail)
        # self.creat_pushButton.clicked.connect(self.saveEmail)
        # self.creat_pushButton.clicked.connect(self.relayEmail)
        self.setLayout(layout)
        self.releaseVersion_lineEdit.setText("BYD_E6_SW4.0RC2")
        self.path_lineEdit.setText("e:\MKSProjects\Vision Test Environment\CANoe\BYD_PlatformEKE\Qualification\E6")
        self.setWindowTitle("AutoCreatEmail")
        self.show()

    def generalEmail(self):
        if self.releaseVersion_lineEdit.text() != "" and self.path_lineEdit.text() !="" and self.emailTo_lineEdit.text()!="" and self.testType_comboBox.currentText()!="":
            recipient = self.emailTo_lineEdit.text()
            cc_recipient = self.emailTo_lineEdit.text()
            subject = self.releaseVersion_lineEdit.text()
            body = f"Hello {self.emailP_lineEdit.text()}:\n" \
                   f"   {self.releaseVersion_lineEdit.text()} {self.testType_comboBox.currentText()} feedback 测试结果统计如下，smokeTest通过.\n" \
                   f"报告日志路径如下：{self.path_lineEdit.text()}\\{self.releaseVersion_lineEdit.text()}"
            # 生成邮件并发送
            e.generate_email(recipient, cc_recipient, subject, body)
            reply = QMessageBox.question(self, "提示", "sendOk", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
        else:
            reply = QMessageBox.question(self, "提示","内容为空", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)

    def saveEmail(self):
        if self.releaseVersion_lineEdit.text() != "" and self.path_lineEdit.text() !="" and self.emailTo_lineEdit.text()!="" and self.testType_comboBox.currentText()!="":
            recipient = self.emailTo_lineEdit.text()
            cc_recipient = self.emailTo_lineEdit.text()
            subject = self.releaseVersion_lineEdit.text()
            body = f"Hello {self.emailP_lineEdit.text()}:\n" \
                   f"   {self.releaseVersion_lineEdit.text()} {self.testType_comboBox.currentText()} feedback 测试结果统计如下，smokeTest通过.\n" \
                   f"报告日志路径如下：{self.path_lineEdit.text()}\\{self.releaseVersion_lineEdit.text()}"
            # 生成邮件并发送
            e.save_email_to_draft(recipient, cc_recipient, subject, body)
            reply = QMessageBox.question(self, "提示", "saveOk", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
        else:
            reply = QMessageBox.question(self, "提示","内容为空", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)

    def relayEmail(self):
        if self.releaseVersion_lineEdit.text() != "" and self.path_lineEdit.text() !="" and self.emailTo_lineEdit.text()!="" and self.testType_comboBox.currentText()!="":
            email_subject = self.emailP_lineEdit.text()
            cc_recipient = self.emailTo_lineEdit.text()
            reply_body = f"Hello {self.emailP_lineEdit.text()}:\n" \
                   f"   {self.releaseVersion_lineEdit.text()} {self.testType_comboBox.currentText()} feedback 测试结果统计如下，smokeTest通过.\n" \
                   f"报告日志路径如下：{self.path_lineEdit.text()}\\{self.releaseVersion_lineEdit.text()}"
            # 生成邮件并发送
            e.reply_to_email(email_subject, cc_recipient,reply_body)
            reply = QMessageBox.question(self, "提示", "replyOk", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
        else:
            reply = QMessageBox.question(self, "提示","内容为空", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)

    def closeEvent(self, event):
        reply = QMessageBox.question(self, '确认退出', '确定要退出吗？', QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
        if reply == QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    widget = MyWidget()
    sys.exit(app.exec_())